package com.battle.service.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.battle.pojo.dto.userRoleMp.SelectUserRoleMpDto;
import com.battle.pojo.dto.userRoleMp.UserUnbindRoleDto;
import com.battle.pojo.entity.UserRoleMp;
import com.battle.service.mapper.UserRoleMpMapper;
import com.battle.service.repository.in.UserRoleMpRepository;
import nirvana.core.exceptions.BizException;
import nirvana.core.repository.Repository;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 *
 */
@Service
public class UserRoleMpRepositoryImpl extends Repository<UserRoleMpMapper, UserRoleMp> implements UserRoleMpRepository {

    private final UserRoleMpMapper mapper;

    public UserRoleMpRepositoryImpl(UserRoleMpMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public UserRoleMp createToBefore(UserRoleMp userRoleMp) {

        Long userId = userRoleMp.getUserId();
        Long roleId = userRoleMp.getRoleId();


        UserRoleMp po = null;
        UserRoleMp byPo = null;

        po = new UserRoleMp();
        po.setUserId(userId);
        po.setRoleId(roleId);

        byPo = getByEntity(po);

        if (byPo != null) {
            throw new BizException("角色关联重复");
        }

        //没id就给他加一个
        Long id = userRoleMp.getId();
        if (id == null) {
            userRoleMp.setId(IdGenerator.SNOW.generate());
        }
        return super.createToBefore(userRoleMp);
    }

    /**
     * 获取用户与角色的关联
     *
     * @param dto
     * @return
     */
    @Override
    public List<UserRoleMp> selectUserRoleMp(SelectUserRoleMpDto dto) {

        List<Long> userIds = dto.getUserIds();
        if (CollectionUtils.isEmpty(userIds)) {
            return Collections.EMPTY_LIST;
        }

        Long roleId = dto.getRoleId();
        QueryWrapper<UserRoleMp> wrapper = getWrapper();
        wrapper.eq("is_delete", Boolean.FALSE);
        wrapper.eq("role_id", roleId);
        wrapper.in("user_id", userIds);

        wrapper.select("user_id", "role_id");

        return list(wrapper);
    }

    /**
     * 解除绑定
     *
     * @param dto
     */
    @Override
    public void userUnbindRole(UserUnbindRoleDto dto) {

        Long roleId = dto.getRoleId();
        List<Long> userIds = dto.getUserIds();
        if (CollectionUtils.isEmpty(userIds)) {
            return;
        }

        UserRoleMp po = new UserRoleMp();
        po.setIsDelete(Boolean.TRUE);

        QueryWrapper<UserRoleMp> wrapper = getWrapper();

        wrapper.eq("role_id", roleId);
        wrapper.in("user_id", userIds);
        mapper.delete(wrapper);
    }

    /**
     * 清除用户的所有绑定
     *
     * @param userId
     */
    @Override
    public void clean(Long userId) {
        if (userId==null){
            return;
        }

        UserRoleMp t = new UserRoleMp();
        t.setUserId(userId);
        mapper.delete(getLambdaWrapper(t));
    }
}